﻿using System;
using System.Linq.Expressions;

namespace LeanEngine.Data.Linq
{
    /// <summary>
    /// Extensions for where clauses
    /// </summary>
    public static class WhereExtensions
    {
        /// <summary>
        /// Returns the first of its arguments that is not null. 
        /// Null is returned only if all arguments are null. 
        /// It is often used to substitute a default value for null values when data is retrieved for display.
        /// </summary>
        /// <typeparam name="T">The System.Type of the value.</typeparam>
        /// <param name="source">The value to be evaluated if is null.</param>
        /// <param name="value">The value that used whether the source is null.</param>
        /// <param name="nullValue">The comparison value considered as null value.</param>
        public static T Coalesce<T>(this T source, T value, T nullValue)
        {
            if (source != null) return source;
            else if (value != null && !value.Equals(nullValue)) return value;
            else return nullValue;
        }
    }
}
